package p0;

public class Leetcode_13 {

    public int romanToInt(String s) {
        int n = s.length();
        int romanInt = 0;
        for (int i = 0; i < n; i++) {
            switch (s.charAt(i)) {
                case 'I':
                    romanInt = romanInt + 1;
                    break;
                case 'V':
                    romanInt = romanInt + 5;
                    break;
                case 'X':
                    romanInt = romanInt + 10;
                    break;
                case 'L':
                    romanInt = romanInt + 50;
                    break;
                case 'C':
                    romanInt = romanInt + 100;
                    break;
                case 'D':
                    romanInt = romanInt + 500;
                    break;
                case 'M':
                    romanInt = romanInt + 1000;
                    break;
                default:
                    break;
            }
            if (i != 0) {
                if (((s.charAt(i) == 'V') || (s.charAt(i) == 'X')) && (s.charAt(i - 1) == 'I')) {
                    romanInt = romanInt - 2;
                }
                if (((s.charAt(i) == 'L') || (s.charAt(i) == 'C')) && (s.charAt(i - 1) == 'X')) {
                    romanInt = romanInt - 10 * 2;
                }
                if (((s.charAt(i) == 'D') || (s.charAt(i) == 'M')) && (s.charAt(i - 1) == 'C')) {
                    romanInt = romanInt - 100 * 2;
                }
            }
        }
        return romanInt;
    }

    public static void main(String[] args) {
        Leetcode_13 l = new Leetcode_13();
        System.out.println(l.romanToInt("LVIII"));
    }
}
